package cn.gzsf.coolshark.controller;

import cn.gzsf.coolshark.mapper.ProductMapper;
import cn.gzsf.coolshark.pojo.dto.ProductDTO;
import cn.gzsf.coolshark.pojo.dto.ProductUpdateDTO;
import cn.gzsf.coolshark.pojo.entity.Product;
import cn.gzsf.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RequestMapping("/product/")
@RestController
public class ProductController {
    @Autowired
    private ProductMapper productMapper;
    @RequestMapping("insert")
    public void insert(@RequestBody ProductDTO productDTO){
        System.out.println("productDTO = " + productDTO);
        //把dto数据装进entity
        Product product = new Product();
        BeanUtils.copyProperties(productDTO,product);
        product.setCreated(new Date());
        product.setViewCount(0);
        //创建ProductMapper,实现insert方法
        productMapper.insert(product);
    }

    @RequestMapping("select")
    public List<ProductAdminVO> select(){
        return productMapper.selectForAdmin();
    }
    @RequestMapping("delete")
    public void delete(int id){
       String url =  productMapper.selectUrlById(id);
       new File("d:/files"+url).delete();
       productMapper.deleteById(id);
    }
    @RequestMapping("selectById")
    public ProductUpdateVO selectById(int id){
        return productMapper.selectById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDTO productUpdateDTO){
        //查询url，查询原来的图片
        String url = productMapper.selectUrlById(productUpdateDTO.getId());
        new File("d:/files"+url).delete();
        //增和改都需要将dto转为entity
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO,product);
        productMapper.update(product);
    }

    @RequestMapping("top")
    public List<ProductTopVO> top(){
        return productMapper.selectTop();
    }

    @RequestMapping("index")
    public List<ProductIndexVO> index(){
        return productMapper.selectIndex();
    }

    @RequestMapping("detail")
    public ProductDetailVO detail(int id, HttpSession session){
        //查询session中是否已经存在view+id，
        Object info = session.getAttribute("view" +id);
        if(info==null){//不存在，则说明用户名没有访问过此商品
            //让当前商品的浏览器+1
            productMapper.updateViewCountById(id);
            //既然该用户已经浏览了此商品，并且商品浏览量已经加1，则把view+id存入session,
            // 下次该用户再次访问该商品，则浏览量不再加1
            session.setAttribute("view"+id,"visited");
        }
        return productMapper.selectDetailById(id);
    }

    @RequestMapping("category")
    public List<ProductIndexVO> category(int id){
        return productMapper.selectByCategoryId(id);
    }

    @RequestMapping("search")
    public List<ProductIndexVO> search(String wd){
        return productMapper.searchByWd(wd);
    }
}
